using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace PixelShader
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Matrix world, view, projection;
        VertexPositionTexture[] verts;
        VertexBuffer vertexBuffer;
        Texture2D textureOrigin, textureDestiny;
        //BasicEffect effect;
        Effect effect;
        float time = 0;
        bool sortTexture = true;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            verts = new VertexPositionTexture[4];
            verts[0] = new VertexPositionTexture (new Vector3 (-1, 1, 0 ), new Vector2 (0,0));
            verts[1] = new VertexPositionTexture (new Vector3 ( 1, 1, 0 ), new Vector2 (1,0));
            verts[2] = new VertexPositionTexture (new Vector3 (-1,-1, 0 ), new Vector2 (0,1));
            verts[3] = new VertexPositionTexture (new Vector3 ( 1,-1, 0 ), new Vector2 (1,1));


            vertexBuffer = new VertexBuffer(GraphicsDevice, typeof (VertexPositionTexture), verts.Length, BufferUsage.None);
            vertexBuffer.SetData<VertexPositionTexture> (verts);

            world = Matrix.Identity;
            
            view = Matrix.CreateLookAt(new Vector3 (0, 0, 5), Vector3.Zero, Vector3.Up);

            projection = Matrix.CreatePerspectiveFieldOfView (MathHelper.PiOver4, (float) Window.ClientBounds.Width / (float)Window.ClientBounds.Height, 0.1f, 100);



            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            textureOrigin = Content.Load<Texture2D>(@"textures\trees");
            textureDestiny = Content.Load<Texture2D>(@"textures\Forest");
            //effect = new BasicEffect(GraphicsDevice);
            effect = Content.Load<Effect>(@"Effect\Effects");
        }


        protected override void Update(GameTime gameTime)
        {
            time += gameTime.ElapsedGameTime.Milliseconds * 0.001f;

            if (time > 1.0f)
            {
                time = 0;
                sortTexture = !sortTexture;
            }


            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            GraphicsDevice.SetVertexBuffer(vertexBuffer);
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;
            /*effect.World = world;
            effect.View = view;
            effect.Projection = projection;
            effect.Texture = texture;
            effect.TextureEnabled = true;*/

            effect.CurrentTechnique = effect.Techniques["TechniqueNormal"];
            effect.Parameters["World"].SetValue(world);
            effect.Parameters["View"].SetValue(view);
            effect.Parameters["Projection"].SetValue(projection);
           // effect.Parameters["colorTexture"].SetValue(textureOrigin);
            effect.Parameters["time"].SetValue(time);

            if (sortTexture)
            {
                effect.Parameters["textureOrigin"].SetValue(textureOrigin);
                effect.Parameters["textureDestiny"].SetValue(textureDestiny);
            }
            else
            {
                effect.Parameters["textureOrigin"].SetValue(textureDestiny);
                effect.Parameters["textureDestiny"].SetValue(textureOrigin);
            }


            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                try
                {
                    GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, verts, 0, 1);
                }

                catch (NotSupportedException)
                {
                    GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
                }
            }
            base.Draw(gameTime);


            effect.CurrentTechnique = effect.Techniques["TechniqueNormal"];
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();

                    GraphicsDevice.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, verts, 1, 1);
                
            }
            base.Draw(gameTime);
        }



    }
}

